% 1. Representation = 1vs7
% 1-2-4. Quality of Deliberation = 0.25 | Quantity of Deliberation = 108

clear
DS = zeros(1,10000);
Vr = zeros(1,10000);
Am = zeros(1,10000);

for k = 1:10000
    % 1) Setup
    m1 = zeros(12,12);
    for i = 1:18
        m1(randsample(144,1))=abs(normrnd(0,1))*-1;
    end
    m1;
    M1 = zeros(12,12);
    for i = 1:18
        M1(randsample(144,1))=abs(normrnd(0,1));
    end
    M1;
    M2 = zeros(12,12);
    for i = 1:18
        M2(randsample(144,1))=abs(normrnd(0,1));
    end
    M2;
    M3 = zeros(12,12);
    for i = 1:18
        M3(randsample(144,1))=abs(normrnd(0,1));
    end
    M3;
    M4 = zeros(12,12);
    for i = 1:18
        M4(randsample(144,1))=abs(normrnd(0,1));
    end
    M4;
    M5 = zeros(12,12);
    for i = 1:18
        M5(randsample(144,1))=abs(normrnd(0,1));
    end
    M5;
    M6 = zeros(12,12);
    for i = 1:18
        M6(randsample(144,1))=abs(normrnd(0,1));
    end
    M6;
    M7 = zeros(12,12);
    for i = 1:18
        M7(randsample(144,1))=abs(normrnd(0,1));
    end
    M7;

    % 2) Deliberation
    % (1) Random selection of a speaker S
    for j = 1:108
        r = rand;
        if r < 0.125 
        S = m1;
    elseif r < 0.25
        S = M1;
    elseif r < 0.375
        S = M2;
    elseif r < 0.5
        S = M3;
    elseif r < 0.625
        S = M4;
    elseif r < 0.75
        S = M5;
    elseif r < 0.875
        S = M6;
    else 
        S = M7;
    end
    % (2) Random selection of a non-zero information point
    info = randsample(nonzeros(S),1);
    % (3) Information sharing and learning
    index = find(S==info);
    if S == m1
        disp('nothing happens')
    else 
        m1(index)=m1(index)+0.25*info;
    end
    if S == M1
        disp('nothing happens')
    elseif S == m1 
        M1(index)=M1(index)+0.25*info;
    else 
        M1(index)=M1(index)+0.5*info;
    end
    if S == M2
        disp('nothing happens')
    elseif S == m1 
        M2(index)=M2(index)+0.25*info;
    else 
        M2(index)=M2(index)+0.5*info;
    end
    if S == M3
        disp('nothing happens')
    elseif S == m1 
        M3(index)=M3(index)+0.25*info;
    else 
        M3(index)=M3(index)+0.5*info;
    end
    if S == M4
        disp('nothing happens')
    elseif S == m1 
        M4(index)=M4(index)+0.25*info;
    else 
        M4(index)=M4(index)+0.5*info;
    end
    if S == M5
        disp('nothing happens')
    elseif S == m1 
        M5(index)=M5(index)+0.25*info;
    else 
        M5(index)=M5(index)+0.5*info;
    end
    if S == M6
        disp('nothing happens')
    elseif S == m1 
        M6(index)=M6(index)+0.25*info;
    else 
        M6(index)=M6(index)+0.5*info;
    end
    if S == M7
        disp('nothing happens')
    elseif S == m1 
        M7(index)=M7(index)+0.25*info;
    else 
        M7(index)=M7(index)+0.5*info;
    end
    end

    % 3) Voting
    % (1) Generating policy proposal
    PP = ones(1,12);
    m11 = sum(m1(:,1));
    M11 = sum(M1(:,1));
    M21 = sum(M2(:,1));
    M31 = sum(M3(:,1));
    M41 = sum(M4(:,1));
    M51 = sum(M5(:,1));
    M61 = sum(M6(:,1));
    M71 = sum(M7(:,1));
    PD1 = [m11, M11, M21, M31, M41, M51, M61, M71];
    if sum(PD1>0) >= 6
        PP(1,1) = 1;
    else 
        PP(1,1) = -1;
    end
    m12 = sum(m1(:,2));
    M12 = sum(M1(:,2));
    M22 = sum(M2(:,2));
    M32 = sum(M3(:,2));
    M42 = sum(M4(:,2));
    M52 = sum(M5(:,2));
    M62 = sum(M6(:,2));
    M72 = sum(M7(:,2));
    PD2 = [m12, M12, M22, M32, M42, M52, M62, M72];
    if sum(PD2>0) >= 6
        PP(1,2) = 1;
    else 
        PP(1,2) = -1;
    end
    m13 = sum(m1(:,3));
    M13 = sum(M1(:,3));
    M23 = sum(M2(:,3));
    M33 = sum(M3(:,3));
    M43 = sum(M4(:,3));
    M53 = sum(M5(:,3));
    M63 = sum(M6(:,3));
    M73 = sum(M7(:,3));
    PD3 = [m13, M13, M23, M33, M43, M53, M63, M73];
    if sum(PD3>0) >= 6
        PP(1,3) = 1;
    else 
        PP(1,3) = -1;
    end
    m14 = sum(m1(:,4));
    M14 = sum(M1(:,4));
    M24 = sum(M2(:,4));
    M34 = sum(M3(:,4));
    M44 = sum(M4(:,4));
    M54 = sum(M5(:,4));
    M64 = sum(M6(:,4));
    M74 = sum(M7(:,4));
    PD4 = [m14, M14, M24, M34, M44, M54, M64, M74];
    if sum(PD4>0) >= 6
        PP(1,4) = 1;
    else 
        PP(1,4) = -1;
    end
    m15 = sum(m1(:,5));
    M15 = sum(M1(:,5));
    M25 = sum(M2(:,5));
    M35 = sum(M3(:,5));
    M45 = sum(M4(:,5));
    M55 = sum(M5(:,5));
    M65 = sum(M6(:,5));
    M75 = sum(M7(:,5));
    PD5 = [m15, M15, M25, M35, M45, M55, M65, M75];
    if sum(PD5>0) >= 6
        PP(1,5) = 1;
    else 
        PP(1,5) = -1;
    end
    m16 = sum(m1(:,6));
    M16 = sum(M1(:,6));
    M26 = sum(M2(:,6));
    M36 = sum(M3(:,6));
    M46 = sum(M4(:,6));
    M56 = sum(M5(:,6));
    M66 = sum(M6(:,6));
    M76 = sum(M7(:,6));
    PD6 = [m16, M16, M26, M36, M46, M56, M66, M76];
    if sum(PD6>0) >= 6
        PP(1,6) = 1;
    else 
        PP(1,6) = -1;
    end
    m17 = sum(m1(:,7));
    M17 = sum(M1(:,7));
    M27 = sum(M2(:,7));
    M37 = sum(M3(:,7));
    M47 = sum(M4(:,7));
    M57 = sum(M5(:,7));
    M67 = sum(M6(:,7));
    M77 = sum(M7(:,7));
    PD7 = [m17, M17, M27, M37, M47, M57, M67, M77];
    if sum(PD7>0) >= 6
        PP(1,7) = 1;
    else 
        PP(1,7) = -1;
    end
    m18 = sum(m1(:,8));
    M18 = sum(M1(:,8));
    M28 = sum(M2(:,8));
    M38 = sum(M3(:,8));
    M48 = sum(M4(:,8));
    M58 = sum(M5(:,8));
    M68 = sum(M6(:,8));
    M78 = sum(M7(:,8));
    PD8 = [m18, M18, M28, M38, M48, M58, M68, M78];
    if sum(PD8>0) >= 6
        PP(1,8) = 1;
    else 
        PP(1,8) = -1;
    end
    m19 = sum(m1(:,9));
    M19 = sum(M1(:,9));
    M29 = sum(M2(:,9));
    M39 = sum(M3(:,9));
    M49 = sum(M4(:,9));
    M59 = sum(M5(:,9));
    M69 = sum(M6(:,9));
    M79 = sum(M7(:,9));
    PD9 = [m19, M19, M29, M39, M49, M59, M69, M79];
    if sum(PD9>0) >= 6
        PP(1,9) = 1;
    else 
        PP(1,9) = -1;
    end
    m110 = sum(m1(:,10));
    M110 = sum(M1(:,10));
    M210 = sum(M2(:,10));
    M310 = sum(M3(:,10));
    M410 = sum(M4(:,10));
    M510 = sum(M5(:,10));
    M610 = sum(M6(:,10));
    M710 = sum(M7(:,10));
    PD10 = [m110, M110, M210, M310, M410, M510, M610, M710];
    if sum(PD10>0) >= 6
        PP(1,10) = 1;
    else 
        PP(1,10) = -1;
    end
    m111 = sum(m1(:,11));
    M111 = sum(M1(:,11));
    M211 = sum(M2(:,11));
    M311 = sum(M3(:,11));
    M411 = sum(M4(:,11));
    M511 = sum(M5(:,11));
    M611 = sum(M6(:,11));
    M711 = sum(M7(:,11));
    PD11 = [m111, M111, M211, M311, M411, M511, M611, M711];
    if sum(PD11>0) >= 6
        PP(1,11) = 1;
    else 
        PP(1,11) = -1;
    end
    m112 = sum(m1(:,12));
    M112 = sum(M1(:,12));
    M212 = sum(M2(:,12));
    M312 = sum(M3(:,12));
    M412 = sum(M4(:,12));
    M512 = sum(M5(:,12));
    M612 = sum(M6(:,12));
    M712 = sum(M7(:,12));
    PD12 = [m112, M112, M212, M312, M412, M512, M612, M712];
    if sum(PD12>0) >= 6
        PP(1,12) = 1;
    else 
        PP(1,12) = -1;
    end
    PP;
    % (2) Acceptability of policy proposal
    Am1 = m11*PP(1,1)+m12*PP(1,2)+m13*PP(1,3)+m14*PP(1,4)+m15*PP(1,5)+m16*PP(1,6)+m17*PP(1,7)+m18*PP(1,8)+m19*PP(1,9)+m110*PP(1,10)+m111*PP(1,11)+m112*PP(1,12);
    AM1 = M11*PP(1,1)+M12*PP(1,2)+M13*PP(1,3)+M14*PP(1,4)+M15*PP(1,5)+M16*PP(1,6)+M17*PP(1,7)+M18*PP(1,8)+M19*PP(1,9)+M110*PP(1,10)+M111*PP(1,11)+M112*PP(1,12);
    AM2 = M21*PP(1,1)+M22*PP(1,2)+M23*PP(1,3)+M24*PP(1,4)+M25*PP(1,5)+M26*PP(1,6)+M27*PP(1,7)+M28*PP(1,8)+M29*PP(1,9)+M210*PP(1,10)+M211*PP(1,11)+M212*PP(1,12);
    AM3 = M31*PP(1,1)+M32*PP(1,2)+M33*PP(1,3)+M34*PP(1,4)+M35*PP(1,5)+M36*PP(1,6)+M37*PP(1,7)+M38*PP(1,8)+M39*PP(1,9)+M310*PP(1,10)+M311*PP(1,11)+M312*PP(1,12);
    AM4 = M41*PP(1,1)+M42*PP(1,2)+M43*PP(1,3)+M44*PP(1,4)+M45*PP(1,5)+M46*PP(1,6)+M47*PP(1,7)+M48*PP(1,8)+M49*PP(1,9)+M410*PP(1,10)+M411*PP(1,11)+M412*PP(1,12);
    AM5 = M51*PP(1,1)+M52*PP(1,2)+M53*PP(1,3)+M54*PP(1,4)+M55*PP(1,5)+M56*PP(1,6)+M57*PP(1,7)+M58*PP(1,8)+M59*PP(1,9)+M510*PP(1,10)+M511*PP(1,11)+M512*PP(1,12);
    AM6 = M61*PP(1,1)+M62*PP(1,2)+M63*PP(1,3)+M64*PP(1,4)+M65*PP(1,5)+M66*PP(1,6)+M67*PP(1,7)+M68*PP(1,8)+M69*PP(1,9)+M610*PP(1,10)+M611*PP(1,11)+M612*PP(1,12);
    AM7 = M71*PP(1,1)+M72*PP(1,2)+M73*PP(1,3)+M74*PP(1,4)+M75*PP(1,5)+M76*PP(1,6)+M77*PP(1,7)+M78*PP(1,8)+M79*PP(1,9)+M710*PP(1,10)+M711*PP(1,11)+M712*PP(1,12);

    % (3) Consensus decision rule
    if Am1>=0 && AM1>=0 && AM2>=0 && AM3>=0 && AM4>=0 && AM5>=0 && AM6>=0 && AM7>=0 
        DS(1,k) = 1;
    else 
        DS(1,k) = 0;
    end
    
    % 4) Dependent variables
    Vr(1,k) = sum(PP<0)/12;
    if DS(1,k) == 1
        VrxDS(1,k) = Vr(1,k)*100;
    else
        VrxDS(1,k) = NaN;
    end

    Am(1,k) = sum(sum(Am1))/12;
    if DS(1,k) == 1
        AmxDS(1,k) = Am(1,k);
    else
        AmxDS(1,k) = NaN;
    end

    AM(1,k) = (sum(AM1)+sum(AM2)+sum(AM3)+sum(AM4)+sum(AM5)+sum(AM6)+sum(AM7))/12;
    if DS(1,k) == 1
        AMxDS(1,k) = AM(1,k);
    else
        AMxDS(1,k) = NaN;
    end
end

writematrix(VrxDS','Vrd1t4.xls')

writematrix(AmxDS','Amd1t4.xls')

RAd = AmxDS./AMxDS;
writematrix(RAd','RAd1t4.xls')

mean(DS)*100
nanmean(VrxDS)
nanstd(VrxDS)
nanmean(AmxDS)
nanstd(AmxDS)
nanmean(RAd)*100
nanstd(RAd)*100